package m.common.service;

import m.common.model.Model;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.system.RuntimeData;
import m.system.exception.MException;

import java.sql.SQLException;

public class Service {
	public <T extends Service> T getService(Class<? extends T> clazz){
		return RuntimeData.getService(clazz);
	}
	public Service getService(){
		return RuntimeData.getService(Service.class);
	}

	public <E extends Model> int saveModel(E model) throws Exception {
		return ModelUpdateUtil.saveModel(model);
	}
	public <E extends Model> int saveModel(E model,String... fileds) throws Exception {
		return ModelUpdateUtil.saveModel(model,fileds);
	}
	/**
	 * 增
	 */
	public <E extends Model> int insertModel(E model) throws MException {
		return ModelUpdateUtil.insertModel(model);
	}
	public <E extends Model> void insertModels(E... model) throws MException {
		ModelUpdateUtil.insertModels(model);
	}

	/**
	 * 删
	 */
	public <E extends Model> int deleteModel(E model) throws MException {
		return ModelUpdateUtil.deleteModel(model);
	}
	public <E extends Model> void deleteModels(E... model) throws MException {
		ModelUpdateUtil.deleteModels(model);
	}
	public <E extends Model> int delete(Class<E> clazz, QueryCondition condition) throws MException {
		return ModelUpdateUtil.delete(clazz,condition);
	}

	/**
	 * 改
	 */
	public <E extends Model> int updateModel(E model) throws Exception {
		return ModelUpdateUtil.updateModel(model);
	}
	public <E extends Model> int updateModel(E model,String... fileds) throws Exception {
		return ModelUpdateUtil.updateModel(model,fileds);
	}
	public <E extends Model> void updateModels(E[] model) throws Exception {
		ModelUpdateUtil.updateModels(model);
	}
	public <E extends Model> void updateModels(E[] model,String... fileds) throws Exception {
		ModelUpdateUtil.updateModels(model,fileds);
	}
	public static <E extends Model> int update(E obj,String[] fields,QueryCondition condition) throws MException{
		return ModelUpdateUtil.update(obj,fields,condition);
	}
	/**
	 * 查
	 */
	public <E extends Model> E getModel(E model,String... fieldNames) throws Exception{
		return ModelQueryList.getModel(model,fieldNames);
	}
	public <E extends Model> E getModel(Class<E> clazz,String oid,String... fieldNames) throws Exception{
		return ModelQueryList.getModel(clazz,oid,fieldNames);
	}
	public <E extends Model> E getModel(Class<E> clazz,String[] fieldNames,QueryCondition condition) throws SQLException, MException {
		return ModelQueryList.getModel(clazz,fieldNames,condition);
	}

}
